<template>
  <div class="container">
    <div class="btnClose">
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <AuthInput
      type="text"
      placeholder="用户名/手机号"
      :rule="/^\d{5,11}$/"
      err_message="请输入正确用户名"
      @valChange="setUsername"
    />
    <AuthInput
      type="text"
      placeholder="昵称"
      :rule="/^\d{3,5}$/"
      err_message="请输入正确昵称"
      @valChange="setNickname"
    />
    <AuthInput
      type="password"
      placeholder="密码"
      :rule="/^\d{3,6}$/"
      err_message="请输入正确密码"
      @valChange="setPassword"
    />
    <AuthBtn btnText="注册" @handleClick="register" />
  </div>
</template>

<script>
import AuthInput from "@/components/AuthInput";
import AuthBtn from "@/components/AuthBtn";
export default {
  data() {
    return {
      username: "",
      nickname: "",
      password: "",
    };
  },
  components: {
    // 注册组件
    AuthInput,
    AuthBtn,
  },
  methods: {
    setUsername(newUsername) {
      this.username = newUsername;
    },
    setNickname(newNickname) {
      this.nickname = newNickname;
    },
    setPassword(newPassword) {
      this.password = newPassword;
    },

    register() {
      console.log("按钮子组件通知注册");
      console.log(this.username);
      console.log(this.nickname);
      console.log(this.password);

      if (!this.username || !this.password || !this.nickname) {
        this.$toast.fail("请输入完整信息");
        return;
      }

      this.$axios({
        method: "post",
        url: "http://127.0.0.1:3000/register",
        data: {
          username: this.username,
          nickname: this.nickname,
          password: this.password,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.message == "注册成功") {
          this.$toast.success("注册成功");
        } else {
          this.$toast.fail(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 24/360 * 100vw;
}
.btnClose {
  .iconfont {
    font-size: 28/360 * 100vw;
  }
}
.logo {
  text-align: center;
  .iconfont {
    font-size: 126/360 * 100vw;
    color: #d81e06;
  }
}
</style>